﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using Newtonsoft.Json;
using UnityEngine;

public class DianluProductor : MonoBehaviour {
	private int i; //为导线个数,用于为导线赋值电流

	//这里保存元件与路径的映射表
	public GameObject B;
	public GameObject W;
	public GameObject R;
	public GameObject S;
	public GameObject Bulb;
	public GameObject Ver;
	public GameObject Ier;
	public GameObject VR1;
	public GameObject VR2;
	public GameObject Diode;
	public GameObject DianLiu;
	public string SchematicsPath;

	private List<GameObject> ele_map;
	private GameObject Dianlu; // 电路父对象
	private struct Circuitry {
		public String name;
		public int id;
		public int size;
		public List<float[]> map;
	};
	private Circuitry circui; // 电路图结构体
	private string circui_id;

	//以下列表 储存生成的电路元件//
	private List<ElementControl> ele_B = new List<ElementControl> ();
	private List<ElementControl> ele_W = new List<ElementControl> ();
	private List<ElementControl> ele_R = new List<ElementControl> ();
	private List<ElementControl> ele_S = new List<ElementControl> (); //s[0]为干路开关s[1](s1),(s2)为支路开关
	private List<ElementControl> ele_Bulb = new List<ElementControl> ();
	private List<ElementControl> ele_Ver = new List<ElementControl> ();
	private List<ElementControl> ele_Ier = new List<ElementControl> ();
	private List<ElementControl> ele_VR1 = new List<ElementControl> ();
	private List<ElementControl> ele_VR2 = new List<ElementControl> ();
	private List<ElementControl> ele_Diode = new List<ElementControl> ();

	private List<List<ElementControl>> instance_map;

	//*************************//
	void save () {
		circui.name = "the name";
		circui.id = 12;
		circui.size = 300;
		circui.map = new List<float[]> ();
		// "id","x","y","orie","State"
		float[] ele1 = new float[] { 0, 1, 1, 0, 0 };
		circui.map.Add (ele1);
		ele1 = new float[] { 1, 1, 1, 1, 0 };
		circui.map.Add (ele1);

		string outjson = JsonConvert.SerializeObject (circui);
		print (outjson);
	}
	//创建元件到世界
	//将原件按顺序放入List中
	GameObject CreateObject (float[] item) {
		//id从0开始
		int id = (int) item[0] - 1;
		float x = item[1];
		float y = item[2];
		int orie = (int) item[3];
		int state = (int) item[4];
		Vector3 posi = new Vector3 (x, y, 0); //向右上偏移
		//生成元件
		GameObject clone;
		if (id==7 || id==8) {
			orie = 0;
		}
		clone = Instantiate (ele_map[id], posi, Quaternion.Euler (0, 0, 90 * orie)) as GameObject;
		//置入对应List
		clone.name = clone.name + instance_map[id].Count;
		instance_map[id].Add (clone.GetComponent<ElementControl> ());
		clone.transform.SetParent (Dianlu.transform);
		clone.GetComponent<ElementControl> ().State = state;
		// 电流表 或 电压表?
		if(id==5||id==6){
			var textTransform = clone.transform.Find("TextObj");
			textTransform.localRotation = Quaternion.Euler (0, 0, -90 * orie);
			// 电压表 5 ;  电流表 6
			textTransform.gameObject.GetComponent<TextMesh>().text = id==5?"V":"A";
		}
		// 改为所有原件都有电流子元件
		return clone;
	}

	// 读取指定电路 并将其反序列化 并生成
	public void ProductById(string circui_id="") {
		Transform parent=null; 
		int z = 0;
		if (circui_id == ""){
			//在lib场景里
			circui_id = PlayerPrefs.GetString("circui_id");
			this.circui_id = circui_id;
			z = -10;
			if (circui_id == null){
				// SCAN初始化
				return;
			}
			PlayerPrefs.SetString("circui_id",string.Empty);
		}else{
			//在scan场景里
			print("in scan!");
			parent = GameObject.Find(circui_id).transform;
			if(parent.Find("Dianlu")!=null){
				//已生成对应电路图
				return;
			}
		}
		//	公共部分
		// 创建电路父对象
		print(circui_id);
		Dianlu = new GameObject ("DianLu");
		Dianlu.transform.position = new Vector3 (0, 0, z);
		String circui_json = "";
		DirectoryInfo Schematics = new DirectoryInfo(SchematicsPath);
		foreach (FileInfo file in Schematics.GetFiles ()) {
			if (file.Name == circui_id) {
				circui_json = file.OpenText().ReadToEnd();
				break;
			}
		}
		if (circui_json == string.Empty) {
			print ("文件不存在");
			return;
		}
		// json load
		Circuitry circui = JsonConvert.DeserializeObject<Circuitry>(circui_json);
		// 创建元件
		foreach (float[] item in circui.map) {
			// "id","x","y","orie","State"
			CreateObject (item);
		}

		//绑定
		if (parent != null) {
			Dianlu.transform.SetParent (parent.transform);
			Dianlu.transform.localRotation = Quaternion.Euler (90, 0, 0);
			Dianlu.transform.localPosition = new Vector3 (-0.3f, 0, 0);
			Dianlu.transform.localScale = new Vector3 (0.3f, 0.3f, 0.3f);
		}

	}

	// Use this for initialization
	void Start () {
		circui_id = string.Empty;
		// 初始化ele_map
		ele_map = new List<GameObject> { B, W, R, S, Bulb, Ver, Ier, VR1, VR2, Diode };
		instance_map = new List<List<ElementControl>> { ele_B, ele_W, ele_R, ele_S, ele_Bulb, ele_Ver, ele_Ier, ele_VR1, ele_VR2, ele_Diode };
		// default
		PlayerPrefs.SetString("circui_id","1");
		ProductById();
	}

	// Update is called once per frame
	void Update () {
		float r1, r2;
		switch (circui_id + "asd") {
			case "1": //电流表测电流乙
				ele_Bulb[0].U = ele_B[0].E * ele_S[0].State;
				for (i = 0; i < 6; i++) {
					ele_W[i].I = ele_Bulb[0].I;
				}
				ele_B[0].I = ele_Bulb[0].I;
				ele_Ier[0].I = ele_Bulb[0].I;
				break;
			case "2": //测并联电路中的电流(支路和干路)
				ele_Bulb[0].U = ele_B[0].E * ele_S[0].State;
				ele_Bulb[1].U = ele_B[0].E * ele_S[0].State;
				for (i = 0; i <= 11; i++) {
					if (i <= 6) {
						ele_W[i].I = ele_Bulb[0].I + ele_Bulb[1].I;

					} else if (i <= 9) {
						ele_W[i].I = ele_Bulb[0].I;
					} else
						ele_W[i].I = ele_Bulb[1].I;
				}
				ele_B[0].I = ele_Bulb[0].I + ele_Bulb[1].I;
				ele_Ier[0].I = ele_Bulb[0].I + ele_Bulb[1].I;
				ele_Ier[1].I = ele_Bulb[1].I;
				ele_S[0].I = ele_B[0].I;
				break;
			case "3": //测并联电路中的电流
				ele_Bulb[0].U = ele_B[0].E * ele_S[0].State;
				ele_Bulb[1].U = ele_B[0].E * ele_S[0].State;
				for (i = 0; i <= 13; i++) {
					if (i == 3 || i == 8 || i == 13) {
						ele_W[i].I = ele_Bulb[0].I;
					} else if (i == 5 || i == 6 || i == 12) {
						ele_W[i].I = ele_Bulb[1].I;
					} else {
						ele_W[i].I = ele_Bulb[0].I + ele_Bulb[1].I;
					}
				}
				ele_B[0].I = ele_Bulb[0].I + ele_Bulb[1].I;
				ele_S[0].I = ele_B[0].I;
				break;
			case "4": //测串联电路各点电流（电流表在右侧）
				r1 = ele_Bulb[0].R;
				r2 = ele_Bulb[1].R;
				ele_Bulb[0].U = r1 / (r1 + r2) * ele_B[0].E * ele_S[0].State;
				ele_Bulb[1].U = r2 / (r1 + r2) * ele_B[0].E * ele_S[0].State;
				for (i = 0; i < 9; i++) {
					ele_W[i].I = ele_Bulb[0].I;
				}
				ele_B[0].I = ele_Bulb[0].I;
				ele_Ier[0].I = ele_Bulb[0].I;
				ele_S[0].I = ele_B[0].I;
				break;
			case "5": //测串联电路各点电流（电流表在中间）
				r1 = ele_Bulb[0].R;
				r2 = ele_Bulb[1].R;
				ele_Bulb[0].U = r1 / (r1 + r2) * ele_B[0].E * ele_S[0].State;
				ele_Bulb[1].U = r2 / (r1 + r2) * ele_B[0].E * ele_S[0].State;
				for (i = 0; i < 9; i++) {
					ele_W[i].I = ele_Bulb[0].I;
				}
				ele_B[0].I = ele_Bulb[0].I;
				ele_Ier[0].I = ele_Bulb[0].I;
				ele_S[0].I = ele_B[0].I;
				break;
			case "6": //测串联电路各点电流（电流表在左侧）
				r1 = ele_Bulb[0].R;
				r2 = ele_Bulb[1].R;
				ele_Bulb[0].U = r1 / (r1 + r2) * ele_B[0].E * ele_S[0].State;
				ele_Bulb[1].U = r2 / (r1 + r2) * ele_B[0].E * ele_S[0].State;
				for (i = 0; i < 9; i++) {
					ele_W[i].I = ele_Bulb[0].I;
				}
				ele_B[0].I = ele_Bulb[0].I;
				ele_Ier[0].I = ele_Bulb[0].I;
				ele_S[0].I = ele_B[0].I;
				break;
			case "7": //测串联电路各点电流（无电流表）
				r1 = ele_Bulb[0].R;
				r2 = ele_Bulb[1].R;
				ele_Bulb[0].U = r1 / (r1 + r2) * ele_B[0].E * ele_S[0].State;
				ele_Bulb[1].U = r2 / (r1 + r2) * ele_B[0].E * ele_S[0].State;
				for (i = 0; i < 10; i++) {
					ele_W[i].I = ele_Bulb[0].I;
				}
				ele_B[0].I = ele_Bulb[0].I;
				ele_S[0].I = ele_B[0].I;
				break;
			case "8": //电流表测电流甲
				ele_Bulb[0].U = ele_B[0].E * ele_S[0].State;
				for (i = 0; i < 6; i++) {
					ele_W[i].I = ele_Bulb[0].I;
				}
				ele_B[0].I = ele_Bulb[0].I;
				ele_Ier[0].I = ele_Bulb[0].I;
				ele_S[0].I = ele_B[0].I;
				break;
			case "9": //电流表外接
				ele_Bulb[0].U = ele_B[0].E * ele_S[0].State;
				for (i = 0; i < 4; i++) {
					if (i < 2) {
						ele_W[i].I = ele_Bulb[0].I;
					} else {
						ele_W[i].I = 0;
					}
				}
				ele_B[0].I = ele_Bulb[0].I;
				ele_Ier[0].I = ele_Bulb[0].I;
				ele_Ver[0].U = ele_Bulb[0].U;
				ele_S[0].I = ele_B[0].I;
				break;
			case "10": //伏安法测电阻电路
				r1 = ele_R[0].R;
				r2 = ele_VR1[0].R;
				ele_R[0].U = r1 / (r1 + r2) * ele_B[0].E * ele_S[0].State;
				ele_VR1[0].U = r2 / (r1 + r2) * ele_B[0].E * ele_S[0].State;
				for (i = 0; i < 7; i++) {
					if (i == 2 || i == 3) {
						ele_W[i].I = 0;
					} else {
						ele_W[i].I = ele_R[0].I;
					}
				}
				ele_Ier[0].I = ele_R[0].I;
				ele_Ver[0].U = ele_R[0].U;
				ele_S[0].I = ele_R[0].I;
				ele_B[0].I = ele_S[0].I;
				break;
			case "11": //伏安法测小灯泡电功率
				r1 = ele_Bulb[0].R;
				r2 = ele_VR1[0].R;
				ele_Bulb[0].U = r1 / (r1 + r2) * ele_B[0].E * ele_S[0].State;
				ele_VR1[0].U = r2 / (r1 + r2) * ele_B[0].E * ele_S[0].State;
				for (i = 0; i < 8; i++) {
					if (i != 2 && i != 3)
						ele_W[i].I = ele_Bulb[0].I;
					else
						ele_W[i].I = 0;
				}
				ele_Ier[0].I = ele_Bulb[0].I;
				ele_Ver[0].U = ele_Bulb[0].U;
				ele_B[0].I = ele_Bulb[0].I;
				ele_S[0].I = ele_B[0].I;
				break;
			case "12": //两个小灯泡并联（支路干路开关控制）
				ele_Bulb[0].U = ele_B[0].E * ele_S[1].State * ele_S[0].State;
				ele_Bulb[1].U = ele_B[0].E * ele_S[2].State * ele_S[0].State;
				if (ele_S[1].State * ele_S[2].State != 0) {
					ele_W[6].I = ele_W[10].I = ele_Bulb[0].I;
					ele_W[7].I = ele_W[11].I = ele_Bulb[1].I;
					for (i = 0; i <= 11; i++) {
						if (i != 6 && i != 10 && i != 7 && i != 11)
							ele_W[i].I = ele_Bulb[0].I + ele_Bulb[1].I;
					}
					ele_S[0].I = ele_Bulb[0].I + ele_Bulb[1].I;
					ele_S[1].I = ele_Bulb[0].I;
					ele_S[2].I = ele_Bulb[1].I;
				} else if (ele_S[1].State == 1 && ele_S[2].State == 0) {
					for (i = 0; i <= 11; i++) {
						if (i == 7 || i == 11) {
							ele_W[i].I = 0;
						} else {
							ele_W[i].I = ele_Bulb[0].I;
						}
					}
					ele_S[1].I = ele_Bulb[0].I;
					ele_S[0].I = ele_Bulb[0].I;
					ele_S[2].I = 0;
				} else if (ele_S[2].State == 1 && ele_S[1].State == 0) {
					for (i = 0; i <= 11; i++) {
						if (i == 6 || i == 10) {
							ele_W[i].I = 0;
						} else {
							ele_W[i].I = ele_Bulb[1].I;
						}
					}
					ele_S[2].I = ele_Bulb[1].I;
					ele_S[0].I = ele_Bulb[1].I;
					ele_S[1].I = 0;
				} else {
					for (i = 0; i <= 11; i++) {
						ele_W[i].I = 0;
					}
					ele_S[0].I = ele_S[1].I = ele_S[2].I = 0;
				}
				ele_B[0].I = ele_S[0].I;
				break;
			case "13": //两个小灯泡并联
				ele_Bulb[0].U = ele_B[0].E * ele_S[0].State;
				ele_Bulb[1].U = ele_B[0].E * ele_S[0].State;
				for (i = 0; i < 14; i++) {
					if (i == 6 || i == 10 || i == 13) {
						ele_W[i].I = ele_Bulb[0].I;
					} else if (i == 7 || i == 11 || i == 12) {
						ele_W[i].I = ele_Bulb[1].I;
					} else {
						ele_W[i].I = ele_Bulb[1].I + ele_Bulb[0].I;
					}
				}
				ele_S[0].I = ele_Bulb[0].I + ele_Bulb[1].I;
				ele_B[0].I = ele_S[0].I;
				break;
			case "14": //两个小灯泡串联
				r1 = ele_Bulb[0].R;
				r2 = ele_Bulb[1].R;
				ele_Bulb[0].U = r1 / (r1 + r2) * ele_B[0].E * ele_S[0].State;
				ele_Bulb[1].U = r2 / (r1 + r2) * ele_B[0].E * ele_S[0].State;
				for (i = 0; i < 10; i++) {
					ele_W[i].I = ele_Bulb[0].I;
				}
				ele_S[0].I = ele_Bulb[0].I;
				ele_B[0].I = ele_S[0].I;
				break;
			case "15": //欧姆定律
				ele_R[0].U = ele_B[0].E * ele_S[0].State;
				for (i = 0; i < 8; i++) {
					if (i != 0 && i != 1 && i != 2 && i != 5) {
						ele_W[i].I = 0;
					} else {
						ele_W[i].I = ele_R[0].I;
					}
				}
				ele_Ver[0].U = ele_R[0].U;
				ele_Ier[0].I = ele_R[0].I;
				ele_S[0].I = ele_R[0].I;
				ele_B[0].I = ele_S[0].I;
				break;
			case "16": //欧姆定律在串并联中的应用二
				ele_R[0].U = ele_B[0].E * ele_S[0].State;
				ele_VR1[0].U = ele_B[0].E * ele_S[0].State;
				for (i = 0; i <= 4; i++) {
					ele_W[i].I = ele_R[0].I + ele_VR1[0].I;
				}
				for (i = 13; i <= 15; i++) {
					ele_W[i].I = ele_R[0].I + ele_VR1[0].I;
				}
				ele_W[5].I = ele_W[7].I = ele_W[9].I = ele_W[10].I = ele_W[12].I = ele_VR1[0].I;
				ele_W[6].I = ele_W[8].I = ele_W[11].I = ele_R[0].I;
				ele_S[0].I = ele_R[0].I + ele_VR1[0].I;
				ele_B[0].I = ele_S[0].I;
				break;
			case "17": //欧姆定律在串并联中的应用一
				r1 = ele_R[0].R;
				r2 = ele_VR1[0].R;
				ele_R[0].U = r1 / (r1 + r2) * ele_B[0].E * ele_S[0].State;
				ele_VR1[0].U = r2 / (r1 + r2) * ele_B[0].E * ele_S[0].State;
				for (i = 0; i < 10; i++) {
					ele_W[i].I = ele_R[0].I;
				}
				ele_S[0].I = ele_R[0].I;
				ele_B[0].I = ele_S[0].I;
				break;
			case "18":
				r1 = ele_R[0].R;
				r2 = ele_VR1[0].R;
				ele_R[0].U = r1 / (r1 + r2) * ele_B[0].E * ele_S[0].State;
				ele_VR1[0].U = r2 / (r1 + r2) * ele_B[0].E * ele_S[0].State;
				for (i = 0; i < 8; i++) {
					if (i == 2 || i == 3) {
						ele_W[i].I = 0;
					} else {
						ele_W[i].I = ele_R[0].I;
					}
				}
				ele_Ier[0].I = ele_R[0].I;
				ele_Ver[0].U = ele_R[0].U;
				ele_S[0].I = ele_R[0].I;
				ele_B[0].I = ele_S[0].I;
				break;
			default:
				break;
		}

	}
}